package com.tbit.uqbike.service.base.impl;


import com.tbit.common.constant.OperationPlatform;
import com.tbit.common.entity.UserOpLog;
import com.tbit.uqbike.constant.MapConstant;
import com.tbit.uqbike.constant.RedisConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.business.Point;
import com.tbit.uqbike.object.pojo.Geo;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.base.GeoService;
import com.tbit.uqbike.service.base.RedisService;
import com.tbit.uqbike.service.rpcservice.PurviewService;
import com.tbit.uqbike.webmanager.business.user_op_log.service.UserOpLogService;
import com.tbit.uqbike.webmanager.dao.core.GeoDao;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import com.tbit.utils.GeoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author chenmengran
 * @Description: 运营区域
 * @date 2019\12\11 001115:44
 */
@Service
public class GeoServiceImpl implements GeoService {

	@Autowired
	private GeoDao geoDao;
	@Autowired
	private PurviewService purviewService;
	@Autowired
	private RedisService redisService;
	@Autowired
	private CalibrateService calibrateService;
    @Autowired
    private UserOpLogService userOpLogService;

	@Override
	public void insert(Geo geo, LoginInfo loginInfo) {
		if (!purviewService.purviewCheck(loginInfo.id, geo.getAccountId())) {
			throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
		}
		Geo geoDB = getByAccountId(geo.getAccountId());
		if (geoDB != null) {
			throw new BaseException("ver.geo.isExist");
		}
		geo.setUpdateTime(new Date());
		//计算电子围栏的中心点
		checkCenter(geo);
		//设置断电时间默认为0
		geo.setCutTimeLong(0);
		geoDao.insert(geo);
	}

	@Override
	public void edit(Geo geo, LoginInfo loginInfo) {
		if (!purviewService.purviewCheck(loginInfo.id, geo.getAccountId())) {
			throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
		}
		geo.setUpdateTime(new Date());
		//计算电子围栏的中心点
		checkCenter(geo);
		//设置断电时间默认为0
		geo.setCutTimeLong(0);
		geoDao.update(geo);
		Geo geoDB = geoDao.select(geo.getGeoId());
		/**清除redis数据*/
		redisService.del(RedisConstant.GEO_KEY_PREFIX + geoDB.getAccountId());
		//System.out.println("清除电子围栏缓存");

		// 插入用户操作日志
		userOpLogService.insertSingle(UserOpLog.builder()
				.accountId(geoDB.getAccountId())
				.opUser(loginInfo.getName())
				.opUserPhone(loginInfo.getPhone())
				.operation("编辑服务区")
				.opPlatform(OperationPlatform.MANAGER.code)
				.opTime(LocalDateTime.now())
				.result(true)
				.build());
	}

	@Override
	public void del(Integer geoId, LoginInfo loginInfo) {
		Geo geo = geoDao.select(geoId);
		if (geo == null) {
			return;
		}
		//权限验证
		if (!purviewService.purviewCheck(loginInfo.id, geo.getAccountId())) {
			throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
		}
		geoDao.del(geoId);
	}

	@Override
	public List<Geo> getByAccountId(Integer accountId, LoginInfo loginInfo) {
		//权限验证
		if (!purviewService.purviewCheck(loginInfo.id, accountId)) {
			throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
		}

		return geoDao.selectByAccountId(accountId);
	}

	@Override
	public List<Geo> getExportByAccountId(Integer accountId) {
        return geoDao.getExportByAccountId(accountId);
	}

	@Override
	public Geo getByAccountId(Integer accountId) {
		return geoDao.getByAccountId(accountId);
	}

	@Override
	public Geo getByGeoId(Integer geoId) {
		return geoDao.getById(geoId);
	}

	@Override
	public void importGeo(MultipartFile file, Integer accountId,Integer mapType) {
		List<Geo> geoList = ExcelUtil.readAll(file, Geo.class);

		// 填充默认值
		geoList.forEach(x -> {
			x.setPoints(x.getPointsC());
			x.setAccountId(accountId);
			// 填充默认值
			x.setGeoType(1);
			x.setGeoSwitch(1);
			x.setCutSwitch(1);
			x.setRecoverSwitch(1);
			x.setLockForceSwitch(1);
			x.setCutTimeLong(0);
			x.setUpdateTime(new Date());
			calibrateService.calGeo(x, mapType, MapConstant.MAP_ORI);

			Geo oldGeo = getByAccountId(accountId);
			if (Objects.nonNull(oldGeo)){
				geoDao.del(oldGeo.getGeoId());
			}
			checkCenter(x);
		});

		geoDao.insertBatch(geoList);
	}


	private void checkCenter(Geo geo) {
		if (geo.getGeoType() == 0) {
			/**圆形电子围栏 */
			Point point = GeoUtil.getPoints(geo.getPoints()).get(0);
			geo.setCenterLa(point.getY());
			geo.setCenterLo(point.getX());
		} else {
           /* List<Coordinate> points = GeoUtil.getCoordinate(geo.getPoints());
            Point newPoint = GeoUtil.getCenterOfGravityPoint4(points);*/
			List<Point> points = GeoUtil.getPoints(geo.getPoints());
			Point newPoint = GeoUtil.GetCenterPoint(points);
			geo.setCenterLa(newPoint.getY());
			geo.setCenterLo(newPoint.getX());
		}
	}

}
